package com.changgou.seckill.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 秒杀订单延时队列
 */
@Configuration
public class QueueConfig {
    /**
     * 延时队列
     * @return
     */
    @Bean
    public Queue delaySeckillQueue(){
        return QueueBuilder.durable("delaySeckillQueue")
                //当前队列消息过期则进入死信
                .withArgument("x-dead-letter-exchange", "seckillExchange")        // 消息超时进入死信队列，绑定死信队列交换机
                .withArgument("x-dead-letter-routing-key", "seckillQueue")   // 绑定指定的routing-key
                .build();
    }

    /**
     * 真正的监听队列
     * @return
     */
    @Bean
    public Queue seckillQueue(){
        return new Queue("seckillQueue");
    }
    @Bean
    public Exchange seckillExchange(){
        return new DirectExchange("seckillExchange");
    }

    /***
     * 交换机与队列绑定
     * @return
     */
    @Bean
    public Binding basicBinding(Queue seckillQueue, Exchange seckillExchange) {
        return BindingBuilder.bind(seckillQueue)
                .to(seckillExchange)
                .with("seckillQueue")
                .noargs();
    }
}
